package com.niezhipeng.routing;

import com.niezhipeng.utils.ConnectFactoryUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class EmitLogDirect {

    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        ConnectFactoryUtils.setFactoryConfig(factory);
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

            // 发送一条info消息
            String severityInfo = "info";
            String messageInfo = "this is a info log";

            channel.basicPublish(EXCHANGE_NAME, severityInfo, null, messageInfo.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + severityInfo + "':'" + messageInfo + "'");

            // 发送一条error消息
            String severityError = "error";
            String messageError = "this is a error log";

            channel.basicPublish(EXCHANGE_NAME, severityError, null, messageError.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + severityError + "':'" + messageError + "'");
        }
    }

}

